//
//  GymManagermentRootController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/5.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "GymManagermentRootController.h"
#import "I_Gym.h"
#import "GymManagermentListCell.h"
#import "EditGymViewController.h"

@interface GymManagermentRootController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation GymManagermentRootController

#pragma mark Class Method
+ (instancetype)controller
{
    GymManagermentRootController *vc = [[GymManagermentRootController alloc] init];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"门店管理";
    [self setRightBarButtonWithTitle:@"新增门店" target:self action:@selector(addStoreBarBtnClicked)];
    
    [self configUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestStoreList];
}

#pragma mark Config/UI
- (void)configUI
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = TP.controllerBackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.insetBottom = 25;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GymManagermentListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GymManagermentListCell class])];
    _tableView.tableFooterView = [UIView new];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark NET
- (void)requestStoreList
{
    if (_dataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    [I_Gym requestGymListOnSuccess:^(NSMutableArray *gymList) {
        [weakself.view hideStatusView];
        weakself.dataSource = gymList;
        [weakself.tableView reloadData];
    } failure:^(NSError *error) {
        if (weakself.dataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestStoreList];
            }];
        } else {
            [AV showError:error];
        }
    }];
}


#pragma mark Action
- (void)addStoreBarBtnClicked
{
    EditGymViewController  *addGymVC = [EditGymViewController controllerWithGym:nil];
    [self.navigationController pushViewController:addGymVC animated:YES];
}


#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GymManagermentListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GymManagermentListCell class])];
    [cell refreshCellWithStore:_dataSource[indexPath.section]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 118;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EditGymViewController  *addGymVC = [EditGymViewController controllerWithGym:_dataSource[indexPath.section]];
    [self.navigationController pushViewController:addGymVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


@end
